<?php
class CountAction extends CommonAction {

    public function index()
    {
        $count = M('Count');
        $nativeCount = M('Native_count');
        $foreignCount = M('Foreign_count');

        $this->_generWeek($count, $nativeCount, $foreignCount);
        $this->_generMonth($count, $nativeCount, $foreignCount);
        $this->_generYear($count, $nativeCount, $foreignCount);

        // 获取周平均
        $todayWeek = date('w');
        $right = (5 - $todayWeek) * 3600 * 24;
        $left = ($todayWeek - 1) * 3600 * 24;

        $endWeekDate = date('Y-m-d', (time() + $right));
        $startWeekDate = date('Y-m-d', (time() - $left));
        $week = $count->where("start_time='$startWeekDate' and end_time='$endWeekDate'")->find();

        // 获取月平均
        $endMonthDate = date('Y-m') . '-28';
        $startMonthDate = date('Y-m') . '-01';
        $month = $count->where("start_time='$startMonthDate' and end_time='$endMonthDate'")->find();

        // 获取年平均
        $endYearDate = date('Y') . '-12-31';
        $startYearDate = date('Y') . '-01-01';
        $year = $count->where("start_time='$startYearDate' and end_time='$endYearDate'")->find();

        $this->assign(
            array(
                'week' => $week['avg_fee'],
                'month' => $month['avg_fee'],
                'year' => $year['avg_fee'],
            )
        );

        $this->display();
    }

    /** 形成周表
     * @param Model $count
     * @param Model $nativeCount
     * @param Model $foreignCount
     */
    private function _generWeek(Model $count, Model $nativeCount, Model $foreignCount)
    {
        // 周统计表的形成
        $todayWeek = date('w');
        $right = (5 - $todayWeek) * 3600 * 24;
        $left = ($todayWeek - 1) * 3600 * 24;

        $endWeekDate = date('Y-m-d', (time() + $right));
        $startWeekDate = date('Y-m-d', (time() - $left));

        if (!$count->where("start_time='$startWeekDate' and end_time='$endWeekDate'")->find())
        {
            $weekData['start_time'] = $startWeekDate;
            $weekData['end_time'] = $endWeekDate;

            $count->add($weekData);
        }

        // 如果今天过了这一周
        if ((strtotime(date('Y-m-d')) - (strtotime($endWeekDate) + 3600)) > 0)
        {
            $nativeWeek = $nativeCount->field('number')->where("date between '$startWeekDate' and '$endWeekDate'")->select();

            $nativeNumber = 0;
            foreach ($nativeWeek as $k => $v)
            {
                $nativeNumber += $v['number'];
            }

            $foreignWeek = $foreignCount->field('number')->where("date between '$startWeekDate' and '$endWeekDate'")->select();

            $foreignNumber = 0;
            foreach ($foreignWeek as $k => $v)
            {
                $foreignNumber += $v['number'];
            }

            $pay = M('Pay');
            $payData = $pay->find();

            $countFee = $foreignNumber * $payData['foreign'] + $nativeNumber * $payData['native'];

            $avgFee = $countFee / 5;

            $temp = $count->where("start_time='$startWeekDate' and end_time='$endWeekDate'")->find();

            $data['id'] = $temp['id'];
            $data['avg_fee'] = $avgFee;

            $count->save($data);
        }
    }

    /** 形成月表
     * @param Model $count
     * @param Model $nativeCount
     * @param Model $foreignCount
     */
    private function _generMonth(Model $count, Model $nativeCount, Model $foreignCount)
    {
        // 月统计表的形成
        $endDate = date('Y-m') . '-28';
        $startDate = date('Y-m') . '-01';

        if (!$count->where("start_time='$startDate' and end_time='$endDate'")->find())
        {
            $MonthData['start_time'] = $startDate;
            $MonthData['end_time'] = $endDate;

            $count->add($MonthData);
        }

        // 如果今天过了这一周
        if ((strtotime(date('Y-m-d')) - (strtotime($endDate) + 3600)) > 0)
        {
            $nativeWeek = $nativeCount->field('number')->where("date between '$startDate' and '$endDate'")->select();

            $nativeNumber = 0;
            foreach ($nativeWeek as $k => $v)
            {
                $nativeNumber += $v['number'];
            }

            $foreignWeek = $foreignCount->field('number')->where("date between '$startDate' and '$endDate'")->select();

            $foreignNumber = 0;
            foreach ($foreignWeek as $k => $v)
            {
                $foreignNumber += $v['number'];
            }

            $pay = M('Pay');
            $payData = $pay->find();

            $countFee = $foreignNumber * $payData['foreign'] + $nativeNumber * $payData['native'];

            $avgFee = $countFee / 20;

            $temp = $count->where("start_time='$startDate' and end_time='$endDate'")->find();

            $data['id'] = $temp['id'];
            $data['avg_fee'] = $avgFee;

            $count->save($data);
        }
    }

    /** 形成年表
     * @param Model $count
     * @param Model $nativeCount
     * @param Model $foreignCount
     */
    private function _generYear(Model $count, Model $nativeCount, Model $foreignCount)
    {
        // 年统计表的形成
        $endDate = date('Y') . '-12-31';
        $startDate = date('Y') . '-01-01';

        if (!$count->where("start_time='$startDate' and end_time='$endDate'")->find())
        {
            $MonthData['start_time'] = $startDate;
            $MonthData['end_time'] = $endDate;

            $count->add($MonthData);
        }

        // 如果今天过了这一周
        if ((strtotime(date('Y-m-d')) - (strtotime($endDate) + 3600)) > 0)
        {
            $nativeWeek = $nativeCount->field('number')->where("date between '$startDate' and '$endDate'")->select();

            $nativeNumber = 0;
            foreach ($nativeWeek as $k => $v)
            {
                $nativeNumber += $v['number'];
            }

            $foreignWeek = $foreignCount->field('number')->where("date between '$startDate' and '$endDate'")->select();

            $foreignNumber = 0;
            foreach ($foreignWeek as $k => $v)
            {
                $foreignNumber += $v['number'];
            }

            $pay = M('Pay');
            $payData = $pay->find();

            $countFee = $foreignNumber * $payData['foreign'] + $nativeNumber * $payData['native'];

            $avgFee = $countFee / 365;

            $temp = $count->where("start_time='$startDate' and end_time='$endDate'")->find();

            $data['id'] = $temp['id'];
            $data['avg_fee'] = $avgFee;

            $count->save($data);
        }
    }

}